<?php
// 课程模型
class AccountModel extends CommonModel {
	
	protected $_validate = array(
	// 			array('XS_XH','','学号已经存在！',0,'unique',1), // 在新增的时候验证字段是否唯一
	//         array('XS_XM','require','姓名不能为空'),
	//         array('XS_RXRQ','require','入学日期不能为空'),
	//         array('XS_XX','require','就读学校不能为空'),
	//         array('XS_NJ','require','就读年级不能为空'),
	//         array('XS_BJ','require','就读班级不能为空'),
	//         array('XS_JTZZ','require','家庭住址不能为空'),
	//         array('XS_JTDH','require','家庭电话不能为空'),
	//         array('XS_JZXM','require','家长姓名不能为空'),
	//         array('XS_JZDH','require','家长电话不能为空'),
	//         array('XS_JZMAIL','require','家长Email不能为空'),
	);
	
	protected $_auto =	array(
// 			array('XS_XH','getStuXH',1,'callback'),
// 			array('XS_PY','getStuPY',3,'callback'),
			array('status','1',self::MODEL_INSERT), // 新增的时候把status字段设置为1 活跃
			//array('create_time','time',self::MODEL_INSERT,'function'),
	);
	
	public function getModeDisplayList($xqid,$lb,$CZLX,$CLR,$JE_GREATER,$JE_LESS,$CJSJ_START,$CJSJ_END,$offset,$rows){
		
		$result = array();
		$where ="1";
		if(!empty($xqid)){
			$where .= " and ZM_XQ in(". $xqid .")";
		}
		if(!empty($lb)){
			$where .= " and  ZM_LB='".$lb."'";
		}
		if(!empty($CZLX)){
			$where .= " and  ZM_LB='".$CZLX."'";
		}
// 		if(!empty($CLR)){
			$CLR = mysql_real_escape_string($CLR);
			$where .= " AND ZM_CLR like '%".$CLR."%'"; 
// 		}
		if(!empty($JE_GREATER)){
			$where .= " and  ZM_JE >=".$JE_GREATER;
		}
		if(!empty($JE_LESS)){
			$where .= " and  ZM_JE <=".$JE_LESS;
		}
		if(!empty($CJSJ_START)){
			$where .= " and  ZM_CLSJ >=".$CJSJ_START;
		}
		if(!empty($CJSJ_END)){
			$where .= " and  ZM_CLSJ <=".$CJSJ_END;
		}
		
		$sql = "select t.*,t1.ITEM_VALUE as XQ,t2.ITEM_VALUE AS LB,t3.ITEM_VALUE AS CZLX,t4.nickname AS CLR
					from  (select * from ".$this->tablePrefix."account  where " . $where . " ORDER by id desc) as t
					join ".$this->tablePrefix."dictionary_list t1 on t.zm_xq = t1.ITEM_KEY 
					join ".$this->tablePrefix."dictionary_list t2 on t.zm_lb = t2.ITEM_KEY 
					join ".$this->tablePrefix."dictionary_list t3 on t.zm_czlx = t3.ITEM_KEY 
					join ".$this->tablePrefix."user t4 on t.zm_clrid=t4.id";
		
		$arrNum = $this->db->query("select count(*) as num from ".$this->tablePrefix."account where " . $where);
		$row = $arrNum[0]['num'];
		
		$result["total"] = $row;
		$rs = $this->db->query($sql . " limit $offset,$rows");
// 		$items = array();
// 		while($row = mysql_fetch_object($rs)){
// 			array_push($items, $row);
// 		}
		$result["rows"] = $rs;
		
		return $result;
	}
	
	//自动生成账目流水编号（报名单号）
	public function getAccountLSBH($xq_id,$ZM_CLSJ){
		$ZM_LS = $xq_id . $ZM_CLSJ  ;
		$map['ZM_LS'] = array('like',"%".$ZM_LS."%");
		$Order = M ( "Order" );
		$Orders = array();
		$Orders = $Order->where($map)->limit (0,1)->order ( 'ZM_LS desc' )->select();
		if(count($Orders)!=0){
			$ord = $Orders[0];
			$maxBH = $ord['ZM_LS'];
			$lastNum = substr($maxBH,-2);
			if(substr($lastNum,0,1) == 0 ){ //如果流水号不足两位
				$count = substr($lastNum,-1) + 1 ;
				$ZM_LS = $ZM_LS . '0000'. $count;
			}else{
				$count = $lastNum + 1;
				$ZM_LS = $ZM_LS . '000'. $count;
			}
		}else{
			$ZM_LS = $ZM_LS . '00001' ;
		}
		return  $ZM_LS;
	}
}